package com.interview.pool;

/**
 * 简单的交替输出奇偶数示例（不使用线程池）
 *
 * 功能说明：
 * - 创建两个普通线程
 * - 线程1 输出所有奇数（1, 3, 5, ..., 99）
 * - 线程2 输出所有偶数（2, 4, 6, ..., 100）
 * - 通过 wait/notify 机制确保输出顺序严格按照数字大小递增
 * - 输出结果：1, 2, 3, 4, 5, ..., 98, 99, 100
 *
 * @author Interview
 * @version 1.0
 */
public class SimpleAlternateNumberPrinter1 {

    static class Printer {
        private int currentNumber = 1;
        private static final int MAX_NUMBER = 100;

        private synchronized void printerOdd() {
         while (currentNumber <= MAX_NUMBER){
             if (currentNumber % 2 == 1){
                 System.out.print(currentNumber + " ");
                 currentNumber++;
                 notifyAll();
             }else {
                 try {
                     wait();
                 } catch (InterruptedException e) {
                     throw new RuntimeException(e);
                 }
             }
         }
        }

        private synchronized void printerEven() {
            while (currentNumber <= MAX_NUMBER){
                if (currentNumber % 2 == 0){
                    System.out.print(currentNumber + " ");
                    currentNumber++;
                    notifyAll();
                }else {
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        Printer printer = new Printer();
        Thread oddThread = new Thread(() -> {
            printer.printerOdd();
        },"oddThread");

        Thread evenThread = new Thread(() -> {
            printer.printerEven();
        },"evenThread");

        oddThread.start();
        evenThread.start();
    }


}

